package com.xxgc.ssm.util;

import com.xxgc.ssm.po.info.Info;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.ArrayList;
import java.util.List;

public class ErrorResult {
    //接口的错误提示封装
    public static Info controller(BindingResult result,Info info){
        if (result.hasErrors()) {//如果有异常
            List<String> list = new ArrayList<String>();
            // 将异常信息输出
            info.setCode(-201);
            for (ObjectError s : result.getAllErrors()) {
                list.add(s.getDefaultMessage());
            }
            info.setMsg(list);
        }else{
            info = null;
        }

        return info;
    }
}
